package edu.cuit.zhuyimeng.uaa.config;

import com.alicp.jetcache.Cache;
import com.alicp.jetcache.CacheManager;
import edu.cuit.zhuyimeng.framework.cache.property.RedisProperties;
import edu.cuit.zhuyimeng.framework.cache.util.CacheUtils;
import edu.cuit.zhuyimeng.uaa.controller.vo.UserInfoVO;
import edu.cuit.zhuyimeng.uaa.enums.CacheConstants;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import java.util.List;

/**
 * 缓存配置类
 */
@Configuration
@RequiredArgsConstructor
public class CacheConfig {

    private final CacheManager cacheManager;

    private Cache<String,String> passwordCache;

    private Cache<String, List<String>> permsCache;

    private Cache<String,List<String>> rolesCache;

    private Cache<String, UserInfoVO> userInfoCache;

    @PostConstruct
    public void init() {
        passwordCache = createCache(CacheConstants.PASSWORD_CACHE);
        permsCache = createCache(CacheConstants.PERM_CACHE);
        rolesCache = createCache(CacheConstants.ROLE_CACHE);
        userInfoCache = createCache(CacheConstants.USERINFO_CACHE);
    }

    @Bean
    public Cache<String, String> passwordCache() {
        return passwordCache;
    }

    @Bean
    public Cache<String, List<String>> permsCache() {
        return permsCache;
    }

    @Bean
    public Cache<String, List<String>> rolesCache() {
        return rolesCache;
    }

    @Bean
    public Cache<String, UserInfoVO> userInfoCache() {
        return userInfoCache;
    }

    private <A,B> Cache<A,B> createCache(String name) {
        return cacheManager.getOrCreateCache(CacheUtils.createCommonCache(name));
    }
}
